package com.ouc.openplatform.common;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.joda.time.DateTime;

import com.ouc.openplatform.zykt.orderManage.dao.CS_SensorConfigDao;
import com.ouc.openplatform.zykt.orderManage.model.CS_TestData;
import com.ouc.openplatform.zykt.orderManage.model.CS_TestDataVo;

public class TestDataToTestDataVO {
	private  CS_SensorConfigDao sensorConfigDao;

	public CS_SensorConfigDao getSensorConfigDao() {
		return sensorConfigDao;
	}

	public void setSensorConfigDao(CS_SensorConfigDao sensorConfigDao) {
		this.sensorConfigDao = sensorConfigDao;
	}
	public  CS_TestDataVo TestDataToTestDataVo(CS_TestData TestData) {
		List<String> sensorNoList = new ArrayList<String>();// 传感器编号集合
		List<String> sensorNameList = new ArrayList<String>();// 传感器名称集合
		List<String> sensorValueList = new ArrayList<String>();// 传感器数据集合
		CS_TestDataVo testDataVo = new CS_TestDataVo();
		try {
			BeanUtils.copyProperties(testDataVo, TestData);
			// 切割传感器数据
			String[] split = TestData.getSensorvalue().split("@");
			for (int i = 0; i < split.length; i++) {
				if (!split[i].isEmpty()) {
					String[] sensorInfo = split[i].split(":");
					// 根据传感器编号查询传感器名称
					String sensorName = getSensorNameBySensorNo(sensorInfo[0]);
					if (sensorName != null&&sensorInfo.length>1) {
						// 对传感器名称和数据进行格式化
						sensorInfo[1] = sensorValueFormat(sensorName, sensorInfo[1]);
						sensorName = sensorNameFormat(sensorName);
						sensorNoList.add(sensorInfo[0]);// 添加传感器编号
						sensorValueList.add(sensorInfo[1]);// 添加传感器数据
						sensorNameList.add(sensorName);// 添加传感器名称
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		testDataVo.setDate(TestData.getPrimarykey().substring(0, 10));
		// 计算出当前数据的时间
		StringBuilder sb = new StringBuilder(TestData.getPrimarykey());
		DateTime dt = new DateTime(sb.insert(10, "T").substring(0, 19));
		double seconds = TestData.getHowlong() * 3600;
		// 四舍五入,保留0位小数
		@SuppressWarnings("resource")
		String formatSeconds = new Formatter().format("%.0f", seconds).toString();
		dt = dt.plusSeconds(Integer.parseInt(formatSeconds));

		String nowDataTime = TestData.getPrimarykey().substring(10, 18) + TestData.getHowlong();
		testDataVo.setTime(dt.toString("HH:mm:ss"));
		testDataVo.setTestunitno(Integer.parseInt(TestData.getPrimarykey().substring(18)));
		testDataVo.setSensorNoList(sensorNoList);
		testDataVo.setSensorValueList(sensorValueList);
		testDataVo.setSensorNameList(sensorNameList);
		return testDataVo;
	}
	
	public String getSensorNameBySensorNo(String sensorNo) {
		return sensorConfigDao.getSensorNameBySensorNo(Integer.parseInt(sensorNo));
	}
	/**
	 * 对传感器数据进行格式化
	 * 
	 * @param sensorName
	 * @param sensorValue
	 * @return
	 */
	public static String sensorValueFormat(String sensorName, String sensorValue) {
		if(sensorValue.equals("N")){
			return "N";
		}
		String sensorValueShow = new String();
		double value = Double.parseDouble(sensorValue);
		if ("电压".equals(sensorName)) {
			// 以四舍五入的方式保留两位小数
			sensorValueShow = new Formatter().format("%.1f", value).toString();
		} else if ("电流".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.3f", value).toString();
		} else if ("功率".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.1f", value).toString();
		} else if ("耗电量".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.4f", value).toString();
		} else if ("频率".equals(sensorName)) {
			sensorValueShow = new Formatter().format("%.1f", value).toString();
		} else {
			sensorValueShow = sensorValue;
		}
		return sensorValueShow;

	}

	/**
	 * 对传感器名称进行格式化
	 * 
	 * @param sensorName
	 * @return
	 */
	public static String sensorNameFormat(String sensorName) {
		String sensorNameShow = new String();
		if (sensorName.startsWith("T") || sensorName.startsWith("环温")) {
			sensorNameShow = sensorName + "(℃)";
		} else if ("电压".equals(sensorName)) {
			sensorNameShow = sensorName + "(V)";
		} else if ("电流".equals(sensorName)) {
			sensorNameShow = sensorName + "(A)";
		} else if ("功率".equals(sensorName)) {
			sensorNameShow = sensorName + "(W)";
		} else if ("耗电量".equals(sensorName)) {
			sensorNameShow = sensorName + "(kw.h)";
		} else if ("频率".equals(sensorName)) {
			sensorNameShow = sensorName + "(Hz)";
		} else if ("湿温".equals(sensorName)) {
			sensorNameShow = sensorName + "(℃)";
		} else {
			sensorNameShow = sensorName;
		}
		return sensorNameShow;
	}
public static void main(String[] args) {
	CS_TestData cs_TestData = new CS_TestData();
	cs_TestData.setHowlong(0.0116666666666667);
	cs_TestData.setPrimarykey("2019-04-1203:02:262");
	cs_TestData.setSensorvalue("@1:22.07@2:19.40@3:2.67@4:22.23");
	CS_TestDataVo testDataToTestDataVo = new TestDataToTestDataVO().TestDataToTestDataVo(cs_TestData);
	System.out.println(testDataToTestDataVo);
}
}

